;--------------------------------------
;
; strsrch.asm
;
; insert source into destination
;
;

;--------------------------------------
;
; include defs, prototypes and macros
;

include tab6.inc

;--------------------------------------
;
; begin code segment
;

	.CODE

;--------------------------------------
;
; strsrch(dest,srce);
;
; 

strsrch	PROC PASCAL PUBLIC USES ds es si di,dest:PBYTE,srce:PBYTE
	LOCAL dest_length:WORD,srce_length:WORD
	LOCAL stop_spot:WORD,counter:WORD

; initialize source

 IF mdl eq 3

; set dest es:[di]

        les     di,dest

; set srce ds:[si]

        lds     si,srce

  ELSE

        push    ds
        pop     es

; set dest es:[di]

        mov     di,dest

; set srce ds:[si]

        mov     si,srce

  ENDIF

; get destination string length
	
	sub	cx,cx                  ; set counter to 0
	mov	bx,di                  ; bx becomes offset

	.WHILE 1
	mov	al,es:[bx]
	.BREAK .IF (al==0)
	inc	cx
	inc	bx
	.ENDW

	mov	dest_length,cx

; get source string length

	sub	cx,cx                  ; set counter to 0
	mov	bx,si                  ; bx becomes offset

	.WHILE 1
	mov	al,ds:[bx]
	.BREAK .IF (al==0)
	inc	cx
	inc	bx
	.ENDW

	mov	srce_length,cx

; if source length is greater than
; dest length then not found

        mov     ax,dest_length
        .IF     (cx > ax)
        jmp     not_found
        .ENDIF

; store stop spot

	mov	ax,dest_length
	sub	ax,srce_length
	mov	stop_spot,ax
        inc     stop_spot

; set dest index sounter to 0

        mov     counter,0

; search for srce in dest
      

srch:
	mov	ax,stop_spot
	cmp	ax,counter
	je	not_found


	push	si
	push	di

	cld
repe	cmpsb

	je	is_found

	pop	di
	pop	si

	mov	cx,srce_length

	inc	di
        inc     counter

	jmp	srch


not_found:
	sub	ax,ax
	mov	return_value,ax
        ret

is_found:
	pop	di
	pop	si
	mov	ax,di	
	mov	return_value,ax
	ret	                       ; return to caller
strsrch	ENDP

;
; end of strsrch procedure
;

	END

;
; end of strsrch.asm
;
;--------------------------------------


